﻿using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using xf.Entitys;

namespace xf.Service
{
    public class CustomerService
    {
        private static CustomerService customerService = null;

        /// <summary>
        /// 客户服务层
        /// </summary>
        /// <returns></returns>
        public static CustomerService GetInstance()
        {
            if (customerService == null) {
                customerService = new CustomerService();
            }
            return customerService;
        }

        /// <summary>
        /// 获取所有客户
        /// </summary>
        /// <param name="searchParam">获取所有搜索参数</param>
        /// <param name="like">是否模糊查询</param>
        /// <returns></returns>
        public List<CUS_NAME> GetCustomer(Dictionary<string, string> searchParam, bool like = true)
        {
            ISugarQueryable<CUS_NAME> iSugarQueryable = DbService.GetSqlSugarClient()
                .Queryable<CUS_NAME>()
                .OrderBy(row => row.Cus_id, OrderByType.Desc);
            searchParam.TryGetValue("Cus_id", out string cusId);
            searchParam.TryGetValue("Cus_name", out string cusName);
            if (!string.IsNullOrEmpty(cusId)) {
                if (like)
                {
                    iSugarQueryable
                        .Where(row =>
                            (row.Cus_id == cusId) ||
                            row.Cus_id.Contains(cusId) ||
                            row.Cus_id.StartsWith(cusId) ||
                            row.Cus_id.EndsWith(cusId)
                        );
                }
                else 
                {
                    iSugarQueryable.Where(row => row.Cus_id == cusId);
                }
            }
            if (!string.IsNullOrEmpty(cusName))
            {
                if (like)
                {
                    iSugarQueryable
                        .Where(row =>
                            (row.Cus_name == cusName) ||
                            row.Cus_name.Contains(cusName) ||
                            row.Cus_name.StartsWith(cusName) ||
                            row.Cus_name.EndsWith(cusName)
                        );
                }
                else 
                {
                    iSugarQueryable.Where(row => row.Cus_name == cusName);
                }
                
            }
            return iSugarQueryable.ToList();
        }

        [Obsolete]
        public bool UpdateCustomer(Dictionary<string, string> param, ref Dictionary<string, string> detail)
        {
            bool result = false;
            param.TryGetValue("Cus_id", out string cusId);
            param.TryGetValue("Cus_name", out string cusName);
            if (string.IsNullOrEmpty(cusId) || string.IsNullOrEmpty(cusName))
            {
                detail.Add("msg", "客户编码或客户名称不能为空");
                return result;
            }
            param.Remove("Cus_name");
            List<CUS_NAME> customerResult = GetCustomer(param, false);
            if (customerResult.Count == 0)
            {
                detail.Add("msg", "该客户不存在");
                return result;
            }
            CUS_NAME cusNameObj = customerResult.First<CUS_NAME>();
            if (cusNameObj.Cus_name != cusName) {
                cusNameObj.Cus_name = cusName;
                int count = DbService.GetSqlSugarClient().Updateable<CUS_NAME>()
                    .UpdateColumns(row => new CUS_NAME() { Cus_name = cusNameObj.Cus_name })
                    .Where(row => row.Cus_id == cusNameObj.Cus_id)
                    .ExecuteCommand();
                if (count == 1)
                {
                    result = true;
                }
            }
            return result;
        }

        /// <summary>
        /// 添加用户操作
        /// </summary>
        /// <param name="param"></param>
        /// <param name="detail"></param>
        /// <returns></returns>
        public bool AddCustomer(Dictionary<string, string> param, ref Dictionary<string, string> detail)
        {
            bool result = false;
            param.TryGetValue("Cus_id", out string cusId);
            param.TryGetValue("Cus_name", out string cusName);
            if (string.IsNullOrEmpty(cusId) || string.IsNullOrEmpty(cusName))
            {
                detail.Add("msg", "客户编码或客户名称不能为空");
                return result;
            }
            param.Remove("Cus_name");
            List<CUS_NAME> customerResult = GetCustomer(param, false);
            if (customerResult.Count != 0) {
                detail.Add("msg", "该客户已存在");
                return result;
            }
            CUS_NAME insertObj = new CUS_NAME
            {
                Cus_id = cusId,
                Cus_name = cusName
            };
            result = DbService.GetSqlSugarClient().Insertable(insertObj).ExecuteCommandIdentityIntoEntity();
            return result;
        }

        /// <summary>
        /// 删除指定主键的客户
        /// </summary>
        /// <param name="cusId"></param>
        /// <param name="detail"></param>
        /// <returns></returns>
        public bool RemoveCustomer(string cusId, ref Dictionary<string, string> detail)
        {
            bool result = false;
            if (string.IsNullOrEmpty(cusId)) {
                detail.Add("msg", "客户编码不能为空");
                return result;
            }
            result = (Convert.ToInt32(DbService.GetSqlSugarClient().Deleteable<CUS_NAME>().Where(row => row.Cus_id == cusId).ExecuteCommand().ToString()) == 1);
            return result;
        }
    }
}
